import request from '@/utils/request'
// import { take_array } from './atom/keep-data'
import { get } from '@/utils/storage'

export function add(project_id, spaceid, type, doc_parent_id, doc_title, tables, message_type, params, returns, params_prop, returns_prop) {
  return request({
    url: 'docs/add',
    method: 'post',
    data: {
      project_id,
      doc_parent_id,
      doc_title,
      editer: get('user_code'),
      spaceid,
      type,
      message_type,
      tables: tables ? JSON.stringify(tables) : null,
      params: JSON.stringify(params),
      returns: JSON.stringify(returns),
      params_prop: Array.from(take_array(params_prop, new Map()).values()),
      returns_prop: Array.from(take_array(returns_prop, new Map()).values())
    }
  })
}
export function take_array(arr, results) {
  arr.forEach((item) => {
    if (item.children) {
      results.set(item.prop_id, {
        ...item,
        children: null
      })
      return take_array(item.children, results)
    } else {
      results.set(item.prop_id, {
        ...item,
        children: null
      })
    }
  })
  return results
}

export function edit(doc_id, doc_title, message, tables, params, returns, params_prop, returns_prop) {
  return request({
    url: 'docs/edit',
    method: 'post',
    data: {
      doc_id,
      doc_title,
      editer: get('user_code'),
      message,
      tables: tables ? JSON.stringify(tables) : null,
      params: JSON.stringify(params),
      returns: JSON.stringify(returns),
      params_prop: Array.from(take_array(params_prop, new Map()).values()),
      returns_prop: Array.from(take_array(returns_prop, new Map()).values())
    }
  })
}

export function find_list(spaceid, type) {
  return request({
    url: 'docs',
    method: 'post',
    data: {
      spaceid,
      type
    }
  })
}

export function find_classify(spaceid, type) {
  return request({
    url: 'docs/classify',
    method: 'post',
    data: {
      spaceid,
      type
    }
  })
}

// 查询单条服务数据
export function find_one_api(doc_id) {
  return request({
    url: 'docs/find',
    method: 'post',
    data: {
      doc_id
    }
  })
}
// 根据文档名称 条件查询
export function find_search(doc_id, doc_title, spaceid, type) {
  const data = {
    spaceid,
    type
  }
  if (doc_id) {
    data.doc_id = doc_id
  }
  if (doc_title) {
    data.doc_title = doc_title
  }
  return request({
    url: 'docs/search',
    method: 'post',
    data
  })
}
// 修改文档
export function update(doc_id, doc_title) {
  return request({
    url: 'docs/edit',
    method: 'post',
    data: {
      doc_id,
      doc_title,
      editer: get('user_code')
    }
  })
}

// 新增分类
export function add_classify_service(spaceid, type, doc_parent_id, doc_title) {
  return request({
    url: 'docs/add_classify',
    method: 'post',
    data: {
      spaceid,
      type,
      doc_parent_id,
      doc_title,
      editer: get('user_code')
    }
  })
}

export function del(doc_id, flag) {
  return request({
    url: flag ? 'docs/del_doc' : 'docs/del_classify',
    method: 'post',
    data: {
      doc_id
    }
  })
}

export function get_tables(project_id) {
  return request({
    url: 'projects/get-tables',
    method: 'post',
    data: {
      project_id
    }
  })
}

export function test_api(sessionid, project_id, project_name, message_type, params, tables) {
  return request({
    url: 'projects/testmodel',
    method: 'post',
    data: {
      sessionid, project_id, project_name, message_type, params: JSON.parse(params), tables
    }
  })
}

export function add_question(doc_id, message, editor, params) {
  return request({
    url: 'docs/add-question',
    method: 'post',
    data: {
      doc_id,
      message,
      editor,
      params: JSON.stringify(params)
    }
  })
}

export function attention(doc_id, user) {
  return request({
    url: 'docs/attention',
    method: 'post',
    data: { doc_id, user }
  })
}

export function change_state(question_id, state, user) {
  return request({
    url: 'docs/change_state',
    method: 'post',
    data: {
      question_id,
      state,
      user
    }
  })
}

/**
 * 查询所有项目
 */
export function project_list() {
  return request({
    url: 'projects',
    method: 'post',
    data: {}
  })
}
